package com.xw.musicplayer.floatView;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.WindowManager;

public class FloatWindowManager {

    /**
     * 悬浮窗
     */
    private static FloatLayout mFloatLayout;
    private static WindowManager mWindowManager;
    private static WindowManager.LayoutParams wmParams;
    private static boolean mHasShown;

    /**
     * 创建悬浮窗
     */
    public static void createFlowWindow(Context context) {

        wmParams = new WindowManager.LayoutParams();
        WindowManager windowManager = getWindowManager(context);
        mFloatLayout = new FloatLayout(context);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }

        //设置图片格式，效果为背景透明
        wmParams.format = PixelFormat.RGBA_8888;
        //设置浮动窗口不可聚焦（实现操作除浮动窗口外的其他可见窗口的操作）
        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        //调整悬浮窗显示的停靠位置为左侧置顶
        wmParams.gravity = Gravity.START | Gravity.TOP;

        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        mWindowManager.getDefaultDisplay().getMetrics(dm);
        //窗口的宽度
        int screenWidth = dm.widthPixels;
        //窗口高度
        int screenHeight = dm.heightPixels;
        //以屏幕左上角为原点，设置x、y初始值，相对于gravity
        wmParams.x = screenWidth;
        wmParams.y = screenHeight / 2 + screenWidth / 3;

        //设置悬浮窗口长宽数据
        wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mFloatLayout.setParams(wmParams);
        windowManager.addView(mFloatLayout, wmParams);
    }

    public static void removeFloatWindow() {

        if (mFloatLayout == null) {
            return;
        }

        if (mHasShown && mWindowManager != null) {
            mWindowManager.removeView(mFloatLayout);
        }

        mHasShown = false;
    }

    private static WindowManager getWindowManager(Context context) {
        if (mWindowManager == null) {
            mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }

        return mWindowManager;
    }

    public static void hide() {
        if (mHasShown && mFloatLayout != null) {
            mFloatLayout.hide();
            mHasShown = false;
        }
    }

    public static void show() {
        if (!mHasShown && mFloatLayout != null) {
            mFloatLayout.show();
            mHasShown = true;
        }
    }

    public void setMaxProgress(int length) {
        if (mHasShown && mFloatLayout != null) {
            mFloatLayout.setMaxProgress(length);
        }
    }

    public static void updataProgress(int progress) {
        if (mHasShown && mFloatLayout != null) {
            mFloatLayout.updateProgress(progress);
        }
    }

    public static void setChangeListener() {
        if (mFloatLayout != null) {
            mFloatLayout.setChangeListener();
        }
    }
}
